Page({
  data: {
    current: 'hot',
    hotStatus: 0,
    comingStatus: 0
  },
  onLoad: function () {
    let app = getApp()
    let _this = this
    app.getLocation(function (locate) {
      // console.log(locate)
      _this.setData({locate:locate})
      app.globalData.locate = locate
      // 根据用户所在城市进行数据查询
      wx.request({
        url: 'https://wx.maoyan.com/mmdb/movie/v2/list/hot.json',
        method: 'get',
        data: {
          limit: 12,
          offset: 0,
          ct: locate
        },
        success: function (info) {
          // console.log(info)
          // 热门
          info.data.data.hot.forEach(ele => {
            ele.img = ele.img.replace('w.h','128.180')
          })
          _this.setData({
            hots: {
              items: info.data.data.hot
            }
          })
          _this.setData({hotStatus: 1})
        }
      })
    }) 
  },
  switch: function(event) {
    // console.log(event.target.dataset.current)
    this.setData({
      current: event.target.dataset.current
    })
    let _this = this
    // 当用户点击tab时发送请求（没有数据时）
    // 根据自定义属性data-status的改变来判断是否有数据
    let status = event.target.dataset[event.target.dataset.current + 'Status']
    // console.log(status)

    if (status == 0) {
      // 请求数据
      if (event.target.dataset.current == 'hot') {
        // 请求热门数据
      }
      if (event.target.dataset.current == 'coming') {
        // 请求待映数据
        wx.request({
          url: 'https://wx.maoyan.com/mmdb/movie/v1/list/wish/order/coming.json',
          method: 'get',
          data: {
            ci: 1,
            limit: 30,
            offset: 0,
          },
          success: function (v1) {
            v1.data.data.coming.forEach(ele => {
              ele.img = ele.img.replace('w.h','170.230')
              ele.comingTitle = ele.comingTitle.slice(0,-3)
            })
            // console.log(v1.data.data)
            _this.setData({
              coming: {
                items: v1.data.data.coming
              }
            })
            wx.request({
              url: 'https://wx.maoyan.com/mmdb/movie/v2/list/rt/order/coming.json',
              method: 'get',
              data: {
                ci: 1,
                limit: 10
              },
              success: function (v2) {
                v2.data.data.coming.forEach(ele => {
                  ele.img = ele.img.replace('w.h','128.180')
                })
                _this.setData({comingStatus: 1})
                _this.setData({
                  v1: {
                    items: v1.data.data.coming
                  },
                  v2: {
                    items: v2.data.data.coming
                  }
                })
              }
            }) 
          }
        })
      }
    }
  },
  handleTap (e) {
    var url = '../detail/detail?id=' + e.currentTarget.id
    wx.navigateTo({
      url: url
    })
  }
  // onPullDownRefresh () {
  //   console.log('下拉')
  //   wx.stopPullDownRefresh()
  // }
})